# model

# determine the current user, his credentials and the keys (files) for his bucket
# then list all keys
# is responsible for the user-rights (sets the grant-commands for the db)
#        => maybe it will be better to separate the task with the grant-commands
#            into a own class (SetACL)

from google.appengine.ext import webapp

from controller.BuildAclInfo import *

from Utilities import *


class ViewACL(webapp.RequestHandler):
    def get(self):
        utilities = Utilities()
        
        # get the key-name and md5-checksum
        key = self.request.get('key')
        md5 = self.request.get('md5hash')
        
        # get some basic attributes
        username = utilities.getUsername()
        userText = utilities.getUserText()
        
        if username:
            urlText = utilities.getUrlTextLogout()
            url = utilities.getLogout(self)
            
            # determine the bucketname
            bucketname = utilities.getBucketname(username)
            
            # determine the available storage-service-credentials
            credentials = utilities.getCredentials(username)
            results = credentials.fetch(100)
            
            # initialize the instance-variables
            amazonBucketInstance = None
            googleBucketInstance = None
            openStackBucketInstance = None

            if results:
                for result in results:
                    regionname = result.regionname
                    
                    if regionname == utilities.getAmazonText():
                        amazonStorageService, regionname = utilities.getStorageServiceConnection(username, regionname)
                        amazonBucketInstance = utilities.getBucketInstance(amazonStorageService, bucketname)
                        
                    if regionname == utilities.getGoogleText():
                        googleStorageService, regionname = utilities.getStorageServiceConnection(username, regionname)
                        googleBucketInstance = utilities.getBucketInstance(googleStorageService, bucketname)
                        
                    if regionname == utilities.getOpenStackText():
                        
                        openStackStorageService, regionname = utilities.getStorageServiceConnection(username, regionname)
                        openStackBucketInstance = utilities.getBucketInstance(openStackStorageService, bucketname)
            else:
                self.redirect('/regions')
                
            # fill with empty strings, because there is nothing to print...
            hintText = ''
            hintColor = ''
            
            # prepare the necessary place-holder
            templateValues = {
                              'userText': userText,
                              'user': username,
                              'urlText': urlText,
                              'url': url,
                              'urlText': urlText,
                              'hintText': hintText,
                              'hintColor': hintColor
                              }
                
            # proceeding from the fact, that at this point the storage-services are synchronized, it should be enough to get the bucket-instance of only one storage-service
            if amazonBucketInstance:
                keyInstance = amazonBucketInstance.get_acl(key)
                acl = amazonBucketInstance.get_acl(key_name=key)
                
                self.checkACL(utilities, keyInstance, acl, key, md5, templateValues)
                
            elif googleBucketInstance:
                keyInstance = googleBucketInstance.get_acl(key)
                acl = googleBucketInstance.get_acl(key_name=key)
                
                self.checkACL(utilities, keyInstance, acl, key, md5, templateValues)
                
            elif openStackBucketInstance:
                keyInstance = openStackBucketInstance.get_acl(key)
                acl = openStackBucketInstance.get_acl(key_name=key)

                self.checkACL(utilities, keyInstance, acl, key, md5, templateValues)
        else:
            self.redirect('/')
            
            
    # check the actual acl of the chosen file
    def checkACL(self, utilities, keyInstance, acl, key, md5, templateValues):
        permissions = utilities.checkACL(utilities, keyInstance, acl)
        
        currentACL = self.determineACL(utilities, keyInstance, acl)
                
        self.callBuilder(key, md5, currentACL, templateValues, permissions)
        
    
    # check the acl-document, determine the previous chosen alc-settings and return it
    def determineACL(self, utilities, keyInstance, acl):
        permissions = utilities.checkACL(utilities, keyInstance, acl)
        
        ownerFULL = permissions['OwnerFULL']
        allUsersREAD = permissions['AllUsersREAD']
        allUsersWRITE = permissions['AllUsersWRITE']
        authentUsersREAD = permissions['AuthentUsersREAD']
        
        # private
        if ownerFULL == 'true' and allUsersREAD == 'false' and allUsersWRITE == 'false' and authentUsersREAD == 'false':
            currentACL = 'private'
            return currentACL
        
        # authenticated-read
        elif ownerFULL == 'true' and authentUsersREAD == 'true':
            currentACL = 'authenticated-read'
            return currentACL
        
        # public-read
        elif ownerFULL == 'true' and allUsersREAD == 'true' and allUsersWRITE == 'false':
            currentACL = 'public-read'
            return currentACL
        
        # public-read-write
        elif ownerFULL == 'true' and allUsersREAD == 'true' and allUsersWRITE == 'true':
            currentACL = 'public-read-write'
            return currentACL
        
        # none of the available acl-settings
        else:
            return None
    
        
    # responsible for the call of the get-method of the BuildAclInfo-Class
    def callBuilder(self, key, md5, acl, templateValues, permissions):
        builder = BuildAclInfo()
        builder.get(self, key, md5, acl, templateValues, permissions)


 
        